package cn.hchaojie.camp.javafx.controls;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;

public class DemoTableView extends Application {

	@Override
	public void start(Stage stage) throws Exception {
		Scene scene = new Scene(createContents());
		
		stage.setScene(scene);
		stage.show();
	}
	
	// 在这里创建页面布局
	private Parent createContents() {
		final TableView<Contact> tableView = new TableView<>();
		tableView.setPrefWidth(300);

		final ObservableList<Contact> contacts = FXCollections.observableArrayList();
		contacts.addAll(ContactsData.getContacts());
		tableView.setItems(contacts);

		TableColumn<Contact, String> aliasNameCol = new TableColumn<>("姓名");
		aliasNameCol.setCellValueFactory(new PropertyValueFactory<Contact, String>("name"));
		aliasNameCol.setPrefWidth(tableView.getPrefWidth() / 3);

		TableColumn<Contact, String> firstNameCol = new TableColumn<>("电话");
		firstNameCol.setCellValueFactory(new PropertyValueFactory<Contact, String>("phone"));
		firstNameCol.setPrefWidth(tableView.getPrefWidth() / 3);

		TableColumn<Contact, String> lastNameCol = new TableColumn<>("备注");
		lastNameCol.setCellValueFactory(new PropertyValueFactory<Contact, String> ("note"));
		lastNameCol.setPrefWidth(tableView.getPrefWidth() / 3);

		ObservableList<TableColumn<Contact, ?>> columns = tableView.getColumns();
		columns.add(aliasNameCol);
		columns.add(firstNameCol);
		columns.add(lastNameCol);
		
		return tableView;
	}

	public static void main(String[] args) {
		launch(args);
	}
}
